package cd.safe.school.activity;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;
import cd.safe.school.R;
import cd.safe.school.adapter.EnglishEditAdapter;
import cd.safe.school.bean.FileItem;
import cd.safe.school.bean.Msg;
import cd.safe.school.bean.PatriarchShare;
import cd.safe.school.bean.SharePager;
import cd.safe.school.data.AppData;
import cd.safe.school.util.DateUtil;
import cd.safe.school.util.DownloadUpload;
import cd.safe.school.util.NetTool;
import cd.safe.school.util.Parse;
import cd.safe.school.util.XmlUtil;
import cd.safe.school.util.ZipUtil;

public class EnglishEditActivity extends Activity {
	private ListView english_list;
	public static Handler handler;
	private ProgressDialog pd;
	private String coursepath;
	private String img_path;
	// private Button upload;
	private List<Map<String, Object>> lists = new ArrayList<Map<String, Object>>();
	private List<SharePager> lst = new ArrayList<SharePager>();
	private AppData aData = new AppData(this);
	private SQLiteDatabase db;
	private String music_path;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.englishdictation_03);
		HomeActivity.uag = 1;
		english_list = (ListView) findViewById(R.id.engilsh_list);
		db = aData.getWritableDatabase();
		// upload = (Button) findViewById(R.id.title_butn);
		pd = ProgressDialog.show(this, "", "数据加载中...", true, false);
		pd.setCancelable(true);

		new Thread(new Runnable() {

			@Override
			public void run() {

				lst = Parse.parsepage(1, 1, 7);
				final EnglishEditAdapter adapter = new EnglishEditAdapter(
						EnglishEditActivity.this, lst);
				handler.post(new Runnable() {

					@Override
					public void run() {
						pd.cancel();
						if (lst.size() > 0) {

							english_list.setAdapter(adapter);

						} else {
							Toast.makeText(EnglishEditActivity.this,
									"数据获取失败...", Toast.LENGTH_SHORT).show();
						}
					}
				});

			}
		}).start();

		english_list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					final int position, long id) {
				// Toast.makeText(EnglishEditActivity.this,
				// lst.get(position).toString(), Toast.LENGTH_LONG).show();
				// new Thread(new Runnable() {
				//
				// @Override
				// public void run() {

				Cursor c = db.query("Courseware", null, "courseID=?",
						new String[] { String
								.valueOf(lst.get(position).getId()) }, null,
						null, null);

				if (c.moveToNext()) {

					coursepath = c.getString(c.getColumnIndex("path"));
					if (coursepath != null && !"".equals(coursepath)) {
						File file = new File(coursepath);
						if (!file.exists()) {
							coursepath = Environment
									.getExternalStorageDirectory()
									+ "/safeschool/course/"
									+ lst.get(position).getId() + "/kejian.zip";
							ContentValues values = new ContentValues();
							values.put("path", coursepath);

							db.update(
									"Courseware",
									values,
									"courseID=?",
									new String[] { String.valueOf(lst.get(
											position).getId()) });

							DownloadUpload.downloadefile(lst.get(position)
									.getUrl(), coursepath);

						} else {
							coursepath = Environment
									.getExternalStorageDirectory()
									+ "/safeschool/course/"
									+ lst.get(position).getId() + "/kejian.zip";
							ContentValues values = new ContentValues();
							values.put("path", coursepath);

							db.update(
									"Courseware",
									values,
									"courseID=?",
									new String[] { String.valueOf(lst.get(
											position).getId()) });
							DownloadUpload.downloadefile(lst.get(position)
									.getUrl(), coursepath);

						}
					}

				} else {
					coursepath = Environment.getExternalStorageDirectory()
							+ "/safeschool/course/" + lst.get(position).getId()
							+ "/kejian.zip";

					ContentValues values = new ContentValues();
					values.put("path", coursepath);
					values.put("courseID", lst.get(position).getId());
					values.put("createTime", DateUtil.getCurrentDate());
					values.put("type", lst.get(position).getType());
					db.insert("Courseware", "courseID", values);

					DownloadUpload.downloadefile(lst.get(position).getUrl(),
							coursepath);
				}

				// File f = new File(Environment.getExternalStorageDirectory()
				// + "/safeschool/kejian");
				// if (f.exists()) {
				// f.delete();
				// } else {
				// f.mkdirs();
				// }
				try {

					ZipUtil.upZipSelectedFile(new File(coursepath),
							Environment.getExternalStorageDirectory()
									+ "/safeschool/course/"
									+ lst.get(position).getId());

					XmlUtil.parsexml(Environment.getExternalStorageDirectory()
							+ "/safeschool/course/" + lst.get(position).getId()
							+ "/GZ.xml");
					music_path = Environment.getExternalStorageDirectory()
							+ "/safeschool/course/" + lst.get(position).getId()
							+ "/" + XmlUtil.musicname;
					img_path = Environment.getExternalStorageDirectory()
							+ "/safeschool/course/" + lst.get(position).getId();

				} catch (Exception e) {
					e.printStackTrace();
					handler.sendEmptyMessage(1001);
				}

				// }
				// }).start();

				Message msg = new Message();
				msg.what = 3;
				Bundle bun = msg.getData();
				bun.putString("music_path", music_path);
				bun.putString("img_path", img_path);
				handler.sendMessage(msg);

			}
		});

	}
}
